package code.temp.log;

import java.util.Date;
import java.util.concurrent.*;

/**
 * <b>Description : </b>
 *
 * @author CPF
 * @date 2020/11/23 10:38
 **/
public class ExecutorDemo {

    private static class Runn1 implements Runnable {

        private int id;

        public Runn1(int id) {
            this.id = id;
        }

        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName() + " ==> id: " + id + " start ---" + new Date().getTime());
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + " ==> id: " + id + " end ---" + new Date().getTime());
        }
    }


    public static void main(String[] args) throws InterruptedException {

        final LinkedBlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>();

        ThreadPoolExecutor executor = new ThreadPoolExecutor(3, 3, 60, TimeUnit.SECONDS, workQueue);

        for (int i = 0; i < 50; i++) {
            executor.execute(new Runn1(i));
            System.out.println("add " + i);
        }

        executor.shutdown();
        while (!executor.awaitTermination(1, TimeUnit.SECONDS));
        System.out.println("线程池已经关闭");
    }

}




